cd "C:\Users\Public\Documents\ImmPanelRevis19\"
cd "ImmPanelRevis19\Donnees\select"

capture program drop robustIDyby
program define robustIDyby
version 11
syntax, occp(name) outc(name)

use finalrobust.dta, clear

/* create some outcome variables */
gen ldp=ln(dp/l.dp)
gen dftfy=(dp==360)-(L.dp==360)
gen oshift=(`occp'==0 & L.`occp'==1)

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd
/* residual outcome */
quietly: reg `outc' F_*  if btime==2
predict r`outc' if btime==2, residuals

/* rank in the wage distribution */
drop F_*
egen qinit = fastxtile(rlsnd) if ftreat, by(ze an btime) nq(100)
replace qinit=qinit/100

/* interaction term and instruments */
gen lqinit=L.qinit
gen inter= dimm*L.qinit
gen linter=ldimm*L.qinit
gen inter75= dimm75 * L.qinit
gen linter75=ldimm75*L.qinit
gen intertm2= dimtm2 * L.qinit
gen lintertm2=ldimtm2 *L.qinit

keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
drop nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace

use temp1.dta, clear
/* instrument baseline tm2 */
/* instrument baseline tm2 */
ivreg2 r`outc' (dimm inter = dimm75 inter75) lqinit  [aweight = iw2] if an==1999 | an==2007, cl(ze)
estimates store `occp'75

ivreg2 r`outc' (dimm inter = dimtm2 intertm2) lqinit [aweight = iw2] if an==1982, cl(ze)
estimates store `occp'tm282

ivreg2 r`outc' (dimm inter = dimtm2 intertm2) lqinit [aweight = iw2] if an==1991, cl(ze)
estimates store `occp'tm291

ivreg2 r`outc' (dimm inter = dimtm2 intertm2) lqinit [aweight = iw2] if an==1999, cl(ze)
estimates store `occp'tm299

ivreg2 r`outc' (dimm inter = dimtm2 intertm2) lqinit [aweight = iw2] if an==2007, cl(ze)
estimates store `occp'tm207

end

capture rm "C:\Users\Public\Documents\Tab21\TableA16.rtf"
/* Mobility */
robustIDyby, occp(bcq) outc(lshift)
/* IVSELECT */
esttab bcq75 bcqtm282 bcqtm291 bcqtm299 bcqtm207 ///
using "C:\Users\Public\Documents\Tab21\TableA16.rtf" /// 
, append title("lshift")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
 

 /*************************************/
/* Inflows into the occupation group */
/*************************************/
capture program drop inflowsrobid
program define inflowsrobid
version 11
syntax, occp(name)

use unbalanced.dta, clear
gen orig=(ptreat==1 & `occp'==1 )
/* gen inflow=(dtreat==1 & bc==1 & (L.bc!=1 | L.ize!=ize) & !missing(L.ize)) */
gen inflow=(dtreat==1 & `occp'==1 & L.ize!=ize )

gen reg=substr(ze,1,2)
gen nreg="11" if reg=="11" | reg=="00"
replace nreg="24" if reg=="24"
replace nreg="27" if reg=="26" | reg=="43"
replace nreg="28" if reg=="23" | reg=="25"
replace nreg="32" if reg=="31" | reg=="22"
replace nreg="44" if reg=="42" | reg=="21" | reg=="41"
replace nreg="52" if reg=="52" 
replace nreg="53" if reg=="53" 
replace nreg="75" if reg=="72" | reg=="74" | reg=="54"
replace nreg="76" if reg=="91" | reg=="73"
replace nreg="84" if reg=="83" | reg=="82"
replace nreg="93" if reg=="93" 
replace nreg="94" if reg=="94" 

gen sond=12
replace sond=24 if an==2007
destring nreg, replace
destring ze, replace

save temp1.dta, replace
collapse (sum) norig=orig ninflow=inflow [fweight = sond], by(ze an)
save p1.dta, replace
use temp1.dta, clear


collapse dimm dimm75 ldimm dimtm2 ldimtm2 nreg Y_*, by(ze an)
joinby ze an using p1.dta, unm(m)
gen poid=sqrt(norig)

drop _merge

egen ize=group(ze)
egen time=group(an)
xtset ize time
gen inflr=(D.ninflow)/norig

xi , pre(REG_) i.nreg
xi , pre(CZ_) i.ze

/* IV */
ivreg2 inflr (dimm  = dimtm2 ) Y_* if an==1999 | an==2007, partial(Y_* ) cl(ze)
estimates store `occp'tm9907

ivreg2 inflr (dimm  = dimtm2 ) Y_* if an==2007, partial(Y_* ) cl(ze)
estimates store `occp'tm207

ivreg2 inflr (dimm  = dimtm2 ) Y_* if an==1999, partial(Y_* ) cl(ze)
estimates store `occp'tm299

ivreg2 inflr (dimm  = dimtm2 ) Y_* if an==1991, partial(Y_* ) cl(ze)
estimates store `occp'tm291

ivreg2 inflr (dimm  = dimtm2 ) Y_* if an==1982, partial(Y_* ) cl(ze)
estimates store `occp'tm282

end

inflowsrobid, occp(bcq) 

 /* IVSELECT */
esttab bcqtm9907 bcqtm282 bcqtm291 bcqtm299 bcqtm207 ///
using "C:\Users\Public\Documents\Tab21\TableA16.rtf" /// 
, append title("INFLOWS bcq")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
 
 
capture program drop rlwagesyby
program define rlwagesyby
version 11
syntax, wage(name) occp(name) y1(integer) y2(integer)

/********************/
/* Balanced Sample  */
/********************/
use final.dta, clear

keep if an==`y1' | an==`y2'
gen lsnd=ln((sn/dp))
gen lsn=ln(sn)
gen dlsn=D.lsn
gen dlsnd=D.lsnd
gen dlsnd6=D.lsnd6
gen dlsnd20=D.lsnd20
gen dlsnd30=D.lsnd30

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* residual wages for each year */
quietly: reg `wage' F_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd

gen ldp=ln(dp/l.dp)
gen oshift=(`occp'==0 & L.`occp'==1)
keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in `y2' {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
drop nbze

save iw2.dta, replace
use temp.dta, replace
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace
collapse drlsnd dimm dimm75 dimtm2 Y_*, by(ze an)

/* IV */
display `y1' `y2'
ivreg2 drlsnd (dimm  = dimtm2 ) Y_* , partial(Y_* ) cl(ze) first
estimates store bs`occp'iv`y1'`y2'


end

rlwagesyby, wage(lsnd) occp(bc) y1(1999) y2(2007) 
rlwagesyby, wage(lsnd) occp(bc) y1(1991) y2(1999) 
rlwagesyby, wage(lsnd) occp(bc) y1(1982) y2(1991) 
rlwagesyby, wage(lsnd) occp(bc) y1(1976) y2(1982) 

esttab bsbciv19761982 bsbciv19821991 bsbciv19911999 bsbciv19992007 ///
using "C:\Users\Public\Documents\Tab21\TableA16.rtf" /// 
, append title("wages")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
